package com.rivercloud.filter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;
import org.nutz.mvc.view.ServerRedirectView;

@IocBean
public class LoginFilter implements ActionFilter{

	//zdb_18.3.5_start
	 private String name;
	    private String path;

	    public LoginFilter(String name, String path) {
	        this.name = name;
	        this.path = path;
	    }

	    
	    /*public void doFilter(ServletRequest request)  
	            throws IOException, ServletException {  
	        try{  
	        	String path =request.getLocalAddr();
	        	System.out.println(path);
	        }  
	        catch(Exception e){  
	            System.out.print(e+"@xfilter");  
	        }  
	          
	    }  */
	  //zdb_18.3.5_end
	    
	@Override
	public View match(ActionContext actionContext) {
		
		//zdb_18.3.5_start
		HttpServletRequest request = actionContext.getRequest();
		String path2 = actionContext.getRequest().getRequestURI();
		//System.out.println(path);
		
		String queryString =request.getQueryString();
		
		if(queryString ==null){
			queryString ="";
			}
		
		String realPath =path2+"?"+queryString;
		//System.out.println(realPath);
		
		request.getSession().setAttribute("prePage", realPath);
		

		
		HttpSession session = Mvcs.getHttpSession(false);
    	if (session == null || null == session.getAttribute(name))
            return new ServerRedirectView(path);
		
		
		//zdb_18.3.5_end
		
		/*HttpServletRequest request  = actionContext.getRequest();
		  ServerRedirectView v1=new ServerRedirectView("/index.jsp");  
		  
		  User existUser = (User) request.getSession().getAttribute("existUser");
			if(existUser == null){
				request.setAttribute("msg", "您还没有登录！没有权限访问！");
				
				
				return new JspView("jsp.msg");
				}*/
        return null;  
}  
	
	
	

	/*@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println(1111);
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletRequest rsp = (HttpServletRequest) response;
		User existUser = (User) req.getSession().getAttribute("existUser");
		if(existUser == null){
			req.setAttribute("msg", "您还没有登录！没有权限访问！");
			req.getRequestDispatcher("msg.jsp").forward(req, response);
			
			return;
		}
		chain.doFilter(req, response);
	}

	@Override
	public void destroy() {
		
	}
*/
}
